﻿using System;
using System.Collections.Generic;
using System.CodeDom;
using BMC.ARSystem;

namespace Wicresoft.EKA.NRemedy.CodeGenerate
{
    public class GenerateDiaryProperty : GeneratePropertyBase
    {
        public override void Create(CodeTypeDeclaration classType, string propertyName, string propertyType,
            Limit limit, Dictionary<string, object> attributePropertyValues)
        {
            if (classType == null) throw new ArgumentNullException("classType");
            if (string.IsNullOrEmpty(propertyName)) throw new ArgumentNullException("propertyName");
            if (string.IsNullOrEmpty(propertyType)) throw new ArgumentNullException("propertyType");
            if (attributePropertyValues == null) throw new ArgumentNullException("attributePropertyValues");

            base.Create(classType, propertyName, propertyType, limit, attributePropertyValues);

            GenerateNameResolver gnr = new GenerateNameResolver();
            //set
            string diarySetName = gnr.DiaryPropertyNameSetResolver(propertyName);
            CodeMemberProperty _propertyset = new CodeMemberProperty();
            _propertyset.Name = diarySetName;
            _propertyset.Type = new CodeTypeReference("String");
            _propertyset.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            string fieldNameset = gnr.DiaryFieldNameSetResolver(propertyName);


            _propertyset.SetStatements.Add(new CodeConditionStatement(new CodeBinaryOperatorExpression(new CodePropertySetValueReferenceExpression(), CodeBinaryOperatorType.ValueEquality,
  new CodePrimitiveExpression(null)), new CodeStatement[] { new CodeAssignStatement(new CodeIndexerExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "ValueList"),
                new CodePrimitiveExpression(attributePropertyValues["DatabaseID"])),
                new CodeFieldReferenceExpression(new CodeTypeReferenceExpression("System.DBNull"), "Value"))},
              new CodeStatement[] { new CodeAssignStatement(new CodeIndexerExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "ValueList"),
                new CodePrimitiveExpression(attributePropertyValues["DatabaseID"])),
                new CodePropertySetValueReferenceExpression())}));

            //get
            string diaryGetName = gnr.DiaryPropertyNameGetResolver(propertyName);
            CodeMemberProperty _propertyget = new CodeMemberProperty();
            _propertyget.Name = diaryGetName;
            _propertyget.Type = new CodeTypeReference("BMC.ARSystem.DiaryList");
            _propertyget.Attributes = MemberAttributes.Public | MemberAttributes.Final;
            string fieldNameget = gnr.DiaryFieldNameGetResolver(propertyName);
            _propertyget.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(new CodeTypeReference("BMC.ARSystem.DiaryList"), new CodeIndexerExpression(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "ValueList"),
                new CodePrimitiveExpression(attributePropertyValues["DatabaseID"])))));


            classType.Members.Add(_propertyget);
            classType.Members.Add(_propertyset);

        }
    }
}
